TARGET=blam

APPS=Ctrl FX Gen Op

###############################################################################

EXTLIBDIR=$(shell pkg-config --variable=extlibdir tinyaml)

APPARGS=$(APPS:%=-modpkg apps/%)

#all: .regen .regen_apps $(TARGET)-debug $(TARGET)-release libtinyaml_$(TARGET).so

all: .regen .regen_apps libtinyaml_$(TARGET).so

.regen: arch.dmpa
	dimpaw -genarch .
	touch $@

.regen_apps: $(APPS:%=.regen_%)

build-release:
	mkdir -p build-release

build-debug:
	mkdir -p build-debug

$(APPS:%=.regen_%):.regen_%:apps/%/app.dmpa
	cd $(<:%/app.dmpa=%) && dimpaw -genapp .
	touch $@

$(TARGET)-debug: build-debug arch.dmpa *.c apps/*/*.c
	rm -f build
	ln -s build-debug build
	dimpaw -archpkg . $(APPARGS) -build Debug
	mv build/$(TARGET) $@

$(TARGET)-release: build-release arch.dmpa *.c apps/*/*.c
	rm -f build
	ln -s build-release build
	dimpaw -archpkg . $(APPARGS) -build Release
	mv build/$(TARGET) $@

libtinyaml_$(TARGET).so: build-release arch.dmpa *.c apps/*/*.c
	rm -f build
	ln -s build-release build
	rm -rf build-release/*
	dimpaw -archpkg . $(APPARGS) -build Extension
	mv build/$(TARGET) $@

clean:
	rm -rf build-debug build-release build $(TARGET)-debug $(TARGET)-release

.PHONY: clean

install: install-ext

install-ext:
	cp blam.tinyalib libtinyaml_$(TARGET).so $(EXTLIBDIR)

test:
	cd tests && ../blam-debug symasm.melang test.tinyasm sine.dmp

debug:
	cd tests && gdb --args ../blam-debug symasm.melang test.tinyasm sine.dmp
